<?php  
  include("../includes/connect_db.php");
  include("../includes/core.php"); 

  $bill = $_POST['bill'];
  $idresto = $_POST['idresto'];
  $total = 0;

  $sql_resto_menu = "SELECT * FROM resto_menu WHERE idresto = '$idresto' AND bill = '$bill'";
  $query_resto_menu = mysql_query($sql_resto_menu);
  while ($resto_menu = mysql_fetch_array($query_resto_menu)) {
  	$total = $total + ($resto_menu[jumlah] * $resto_menu[harga]);
  }

  $sql_setting = "SELECT * FROM setting";
  $query_setting = mysql_query($sql_setting);
  $setting = mysql_fetch_array($query_setting);

  $tax_Service = $setting[tax] + $setting[service];
  $total = $total + ($total * $tax_Service / 100);
?>

<div id="form-modal" class="modal hide fade modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-header">
		<a class="close" data-dismiss="modal" aria-hidden="true">&times;</a>
		<h3>Bill Payment</h3>
	</div>
	<div class="modal-body">
		<p>
			<div class="alert alert-info">
			  Total payment is <b id="total_payment"></b>
			</div>
			<div>
				<form id="form_payment">
					<fieldset style="border: 1px solid #DEDEDE;">
						<h4>Payment Method</h4>
						<div class="row-fluid">
							<div class="span12">
								<button id="pay_cash" class="btn span3" data-value="1"><i class="fa fa-money"></i>    Cash</button>
								<button id="pay_cc" class="btn span3" data-value="2" data-tax="<?php echo $setting['credit_card'] ?>"><i class="fa fa-credit-card"></i>    Credit Card</button>
								<button id="pay_voucher" class="btn span3" data-value="3"><i class="fa fa-list-alt"></i>    Voucher</button>
								<button id="pay_service" class="btn span3" data-value="4"><i class="fa fa-tag"></i>    Service Charge</button>
							</div>
						</div>

						<div class="row-fluid" style="margin-top: 30px;">
							<div class="control-group cash_form hide span6">
								<label class="control-label" for="payment_cash">Cash</label>
								<div class="controls">
									<input id="payment_cash" type="text" class="input-large currency al_right">
								</div>
							</div>
							<div class="control-group cash_form hide span6">
								<label class="control-label" for="payment_change">Change</label>
								<div class="controls alert alert-success" style="min-height: 20px;">
									<b><span id="payment_change" class="currency"></span></b>
								</div>
							</div>
							<div class="control-group credit_form hide alert alert-success">
								<label class="control-label" for="payment_credit">
									Payment by Credit Card
									<b><span id="payment_credit" class="currency" style="margin-left: 30px;"></span></b>
								</label>
							</div>
							<div class="control-group voucher_form hide">
								<label class="control-label" for="payment_voucher" style="float: left; margin-right: 10px;">Voucher Code</label>
								<div class="controls" style="float: left;">
									<input id="payment_voucher" type="text" class="input-large">
								</div>
							</div>
							<div class="control-group service_charge_form hide">
								<label class="control-label" for="payment_service_charge" style="float: left; margin-right: 10px;">Room Number</label>
								<div class="controls" style="float: left;">
									<select id="payment_service_charge" class="input-large">
										<option value="">--- Please Select ---</option>
										<?php  
											$sql_fo = "SELECT front_office.*, room.nama AS nama_room FROM front_office 
																INNER JOIN room
																	ON front_office.`idroom` = room.`id`
																WHERE front_office.`is_paid` = 0";
											$query_fo = mysql_query($sql_fo);
											while ($fo = mysql_fetch_array($query_fo)) {
										?>
											<option value="<?php echo $fo[id] ?>"><?php echo $fo[nama_room]." - ".$fo[nama] ?></option>
										<?php
											}
										?>
									</select>
								</div>
							</div>
						</div>
					</fieldset>
				</form>
			</div>
		</p>
	</div>
	<div class="modal-footer">
		<button id="save_payment" class="btn" disabled="disabled">Submit Payment</button>
		<button id="submit_payment" class="btn btn-primary" disabled="disabled">Print Out</button>
		<button class="btn" data-dismiss="modal">Close</button>
	</div>
</div>

<SCRIPT TYPE="text/javascript">
	function validatePrintOut(status) {
		$('#submit_payment').attr('disabled', 'disabled');
		$('#save_payment').attr('disabled', 'disabled');
		if (status) {
			$('#submit_payment').removeAttr('disabled');
			$('#save_payment').removeAttr('disabled');
		}
	}

	function save_payment_to_db() {

	}

	$(document).ready(function() {
		$('#total_payment').autoNumeric('init', {
			aSign: 'IDR ',
			aPad: false
		});
		$('#payment_cash').autoNumeric('init', {
			aSign: 'IDR ',
			aPad: false
		});
		$('#payment_change').autoNumeric('init', {
			aSign: 'IDR ',
			aPad: false
		});
		$('#payment_credit').autoNumeric('init', {
			aSign: 'IDR ',
			aPad: false
		});

		$('#total_payment').autoNumeric('set', <?php echo $total; ?>);

		$('#payment_cash').keyup(function(event) {
			var that = $(this);
			var cash = parseInt(that.autoNumeric('get'));
			var amount = parseInt($('#total_payment').autoNumeric('get'));
			$('#payment_change').autoNumeric('set', 0);
			validatePrintOut(false);
			if ((cash - amount) >= 0) {
				$('#payment_change').autoNumeric('set', (cash - amount));
				validatePrintOut(true);
			};
		});

		$('#pay_cash').click(function(event) {
			event.preventDefault();
			$('#payment_cash').val("");
			$('#payment_change').html("");
			$('.pay_active').removeClass('pay_active');
			$(this).addClass('pay_active');
			$('[class*=_form]').hide();
			validatePrintOut(false);
			$('.cash_form').show();
		});

		$('#pay_cc').click(function(event) {
			event.preventDefault();
			$('.pay_active').removeClass('pay_active');
			$(this).addClass('pay_active');
			$('[class*=_form]').hide();
			var that = $(this);

			validatePrintOut(true);
			var amount = parseInt($('#total_payment').autoNumeric('get'));
			amount += amount * parseInt(that.data("tax")) / 100;
			$('#payment_credit').autoNumeric('set', amount);
			$('.credit_form').show();
		});

		$('#pay_voucher').click(function(event) {
			event.preventDefault();
			$('.pay_active').removeClass('pay_active');
			$(this).addClass('pay_active');
			$('[class*=_form]').hide();
			validatePrintOut(false);
			$('.voucher_form').show();
		});

		$('#pay_service').click(function(event) {
			event.preventDefault();
			$('.pay_active').removeClass('pay_active');
			$(this).addClass('pay_active');
			$('[class*=_form]').hide();
			validatePrintOut(false);
			$('.service_charge_form').show();
		});

		$('#payment_cash').keyup(function(event) {
			var that = $(this);
			var cash = parseInt(that.autoNumeric('get'));
			var amount = parseInt($('#total_payment').autoNumeric('get'));
			$('#payment_change').autoNumeric('set', 0);
			validatePrintOut(false);
			if ((cash - amount) >= 0) {
				$('#payment_change').autoNumeric('set', (cash - amount));
				validatePrintOut(true);
			};
		});

		$('#payment_service_charge').change(function(event) {
			if ($(this).val() != "") {
				validatePrintOut(true);
			} else {
				validatePrintOut(false);
			};
		});

		$('#payment_method').change(function(event) {
			var that = $(this);
			$('[class*=_form]').hide();
			switch(that.val()){
				case '1':
						validatePrintOut(false);
						$('.cash_form').show();
					break
				case '2':
						validatePrintOut(true);
						var amount = parseInt($('#total_payment').autoNumeric('get'));
						amount += amount * parseInt(that.find(":selected").data("tax")) / 100;
						$('#payment_credit').autoNumeric('set', amount);
						$('.credit_form').show();
					break
				case '3':
						validatePrintOut(false);
						$('.voucher_form').show();
					break
			}
		});

		$('#submit_payment').click(function(event) {
			event.preventDefault();

			var formBill = {
				payment: $('.pay_active').data('value'),
				bill: <?php echo $bill ?>,
				idresto: <?php echo $idresto ?>,
				total: <?php echo $total ?>,
				no_voucher: $('#payment_voucher').val(),
				idfront_office: $('#payment_service_charge').val(),
				act: 1
			}

			$.ajax({
				url: '/action/act_nota.php',
				type: 'POST',
				data: formBill,
				cache: false, 
		        success: function(data){
		        	var payment_id = $('.pay_active').data('value');
		        	var queryString = "&payment_id="+payment_id;
		        	if (payment_id == 1) {
								queryString += "&payment_cash="+$('#payment_cash').autoNumeric('get');
		        	} else if (payment_id == 2) {
								queryString += "&credit_tax="+$('#pay_cc').data("tax");
		        	} else if (payment_id == 3) {
								queryString += "&voucher_code="+$('#payment_voucher').val();
		        	}

		        	alert("Data has been successfully saved.");

		        	if (payment_id != 4) {
		        		// window.open("resto_print_out.php?idresto=<?php echo $idresto; ?>&bill=<?php echo $bill; ?>"+queryString, "popupWindow", "width=400,height=600,scrollbars=yes");
		        		$.get("resto_print_out_format.php?idresto=<?php echo $idresto; ?>&bill=<?php echo $bill; ?>"+queryString, function(dataNew) {
						var contentNew = dataNew;
						findPrinter("EC Printer EC-520");
						setTimeout(function(){
							// printToFile(contentNew);
							printESCP(contentNew);
							// printHTML(contentNew);
							setTimeout(function(){
		        					window.location.reload();
							}, 500);
						}, 1000);
					});
		        	};
		        }
			})
		});

		$('#save_payment').click(function(event) {
			event.preventDefault();

			var formBill = {
				payment: $('.pay_active').data('value'),
				bill: <?php echo $bill ?>,
				idresto: <?php echo $idresto ?>,
				total: <?php echo $total ?>,
				no_voucher: $('#payment_voucher').val(),
				idfront_office: $('#payment_service_charge').val(),
				act: 1
			}

			$.ajax({
				url: '/action/act_nota.php',
				type: 'POST',
				data: formBill,
				cache: false, 
        success: function(data){
        	var payment_id = $('.pay_active').data('value');
        	var queryString = "&payment_id="+payment_id;
        	if (payment_id == 1) {
						queryString += "&payment_cash="+$('#payment_cash').autoNumeric('get');
        	} else if (payment_id == 2) {
						queryString += "&credit_tax="+$('#pay_cc').data("tax");
        	} else if (payment_id == 3) {
						queryString += "&voucher_code="+$('#payment_voucher').val();
        	}

        	alert("Data has been successfully saved.");
        	window.location.reload();
        }
			})
		});
	});
</SCRIPT>